package vaani.internal.actionmanagers;

import java.util.LinkedList;
import java.util.List;
import vaani.internal.ProcessInstance;

/**
 *
 * @author sourcemorph
 */

/*this class is meant to store a collection of two different application managers*/
public class AggregateManager implements ActionManager {

    private List<ActionManager> managerList;
    private List<ProcessInstance> instances;

    public AggregateManager() {
        managerList = new LinkedList<ActionManager>();
    }

    /*method to merge an object of any subclass of actionmanager to the AggregateManager*/
    public boolean mergeActionManager(ActionManager toMerge) {
        managerList.add(toMerge);
        return true;
    }

    public List<ProcessInstance> getProcessInstance() {
        instances = new LinkedList<ProcessInstance>();
        for(ActionManager am : managerList) {
            List<ProcessInstance> tempList = am.getProcessInstance();
            if(tempList != null && tempList.size() > 0) {
                instances.addAll(tempList);
            }
        }
        return instances;
    }

    public boolean runProcess(int x) {
        int temp = 0;
        for(ActionManager am : managerList) {
            int tempSize = 0;
            if(am.getProcessInstance() != null) {
                tempSize = am.getProcessInstance().size();
            }
            if((temp + tempSize) > x) {
                return am.runProcess(x - temp);
            }
            temp += tempSize;
        }
        return false;
    }

    public boolean runProcess() {
        return runProcess(0);
    }
}
